{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2db31a52-c80a-46be-a2a2-14a9d5a4ac94",
   "metadata": {},
   "source": [
    "#第三节课读书笔记"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bfdb6013-bd26-457d-bbe9-4ad28f1fd005",
   "metadata": {},
   "source": [
    "##Python中取变量的命名法\n",
    "\n",
    "Pyton中取变量的名字：字母+数字+‘_’组成，第一个字不能是数字，不能和保留字相同\n",
    "\n",
    "在未来命名中有如下要求：\n",
    "1.尽可能不要用无意义的字母命名\n",
    "2.尽可能用英文单词命名变量\n",
    "3如果有多个单词组成变量，使用下划线连接_\n",
    "\n",
    "-程序代码例子：\n",
    "\n",
    "    i = 1\n",
    "    print[i]\n",
    " \n",
    "  ***"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "29adc68f-df0e-457e-9d49-d5a2374330b7",
   "metadata": {},
   "outputs": [],
   "source": [
    "x = 9\n",
    "x-3 =10 #错误\n",
    "9x = 10 #错误\n",
    "_ = 10 #正确"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "695cdf9c-63b4-4ca1-a2c3-0c3b18ef5e65",
   "metadata": {},
   "source": [
    "## Python变量的类型\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "445a9b7e-0eeb-4169-ba49-7a5045c20800",
   "metadata": {},
   "source": [
    "变量类型分为三类:\n",
    "    数值型：整数（int），浮点数（float），复数（complex）布尔值（bool只有True 和False）\n",
    "    字符型：字符串（str）\n",
    "    空：空列表，空字典，None，"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "5ffd44e0-d9d3-4a35-b139-898342957d48",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'float'>\n"
     ]
    }
   ],
   "source": [
    "x=1.\n",
    "print(type(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "1b2cb9e1-febd-4903-ba1e-4003dd9a9e8b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "isinstance(x,float)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "2eda84ee-4869-4f59-a2d9-4f57cf7934f1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 1\n"
     ]
    }
   ],
   "source": [
    "a='1'\n",
    "b=1\n",
    "print(a,b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "b83f8965-3a07-4b23-aea1-9f8d2b098263",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a==b"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2dcfe432-48bb-4bca-8ec7-56ced5881c78",
   "metadata": {},
   "source": [
    "##Python的运算符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8e722ec4-7de0-452b-b1e8-d8a437caea6f",
   "metadata": {},
   "outputs": [],
   "source": [
    "-数值计算：+ - * / \n",
    "**（次方） \n",
    "% （取余数）\n",
    "//（整数除，除完向下取整）\n",
    "+= (a +=2，a= a+2 表示a加上2）\n",
    "==\n",
    "!\n",
    "&(与运算）\n",
    "-字符串运算符： \n",
    "+（两个字符串没有空格地合在一起）\n",
    "*（重复）\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "d95e24ef-879b-41db-8881-52f7f3dcde0f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    }
   ],
   "source": [
    "a =3 & 4\n",
    "print(a)\n",
    "#3和4的二进制中每个位置的数做与运算，1重合的位数相加得到"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "650c73d2-04d9-481f-a663-4a7c881ea77a",
   "metadata": {},
   "source": [
    "0011\n",
    "0100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "30198f28-978f-4b4a-b71c-f387570f6753",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = True\n",
    "b = 2\n",
    "a + b\n",
    "#True的值为1 False的值为0.\n",
    "#布尔值加上整数时，最后的变量成为一个整数\n",
    "#浮点数加上整数，最后的结果是浮点数\n",
    "#浮点数加上一个复数，最后的结果是复数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "c5bd32dd-e475-4d2e-a22a-94e8bc61f522",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1 ==True\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae9280c4-ff95-45b5-83bf-729c8f560860",
   "metadata": {},
   "source": [
    "Python字符串和切片：\n",
    "切片格式：["
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "ca722c72-2dbb-4d5f-9d96-b68ffdca2c08",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'zh'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = 'GuangZhou University'\n",
    "len(a)\n",
    "'Guangzhou'[5:7]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "ff199c23-2f7f-40e7-b95d-1811674b15af",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<string>, line 1)",
     "output_type": "error",
     "traceback": [
      "Traceback \u001b[1;36m(most recent call last)\u001b[0m:\n",
      "\u001b[0m  File \u001b[0;32mF:\\PYTHON\\Lib\\site-packages\\IPython\\core\\interactiveshell.py:3577\u001b[0m in \u001b[0;35mrun_code\u001b[0m\n    exec(code_obj, self.user_global_ns, self.user_ns)\u001b[0m\n",
      "\u001b[1;36m  Cell \u001b[1;32mIn[14], line 5\u001b[1;36m\n\u001b[1;33m    print(eval(deg) + eval(minute)/60 + eval(sec)/3600)\u001b[1;36m\n",
      "\u001b[1;36m  File \u001b[1;32m<string>:1\u001b[1;36m\u001b[0m\n\u001b[1;33m    .0\t2\u001b[0m\n\u001b[1;37m      \t^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "c= \"10\t*\t00 22 37.0\t2\t+63 52.0\t2\t110.0\t17.0\t 5.4\t00 25 25.1\t+64 08 36\"\n",
    "deg = c[7:9]\n",
    "minute = c[10:12]\n",
    "sec = c[13:17]\n",
    "print(eval(deg) + eval(minute)/60 + eval(sec)/3600)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "756a9d2d-38a8-4de0-9014-f48503fec19a",
   "metadata": {},
   "source": [
    "Python的格式化输出\n",
    "三种技术：\n",
    "-&\n",
    "-format\n",
    "—fString\n",
    "\"格式说明':& （显示要输出的变量）\n",
    "&d, &f, &s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "6c9e777a-d1f0-4e7a-8b2b-442618165ab3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.14159265358979\n"
     ]
    }
   ],
   "source": [
    "pi = 3.14159265358979\n",
    "print(pi)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "b003b5ce-e1a8-49f6-b9c9-2b79ba3c2d56",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   3   4\n",
      " 100 101\n",
      "03.14159\n"
     ]
    }
   ],
   "source": [
    "a =3\n",
    "b = 4 \n",
    "c = 100\n",
    "pi = 3.14159265358979\n",
    "print('%4d%4d' % (a,b))\n",
    "print('%4d%4d' % (c,c+1))\n",
    "print('%08.5f' % pi)    #总共八位数，小数点占一位，小数点后面4为数）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c607d0ab-8c6e-40a9-9370-9b2bf0a82940",
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "random_numbers = [random.uniform(0, 100) for i in range(100)]\n",
    "for i in range(0, 100, 10):\n",
    "    print(f'{number:8.3f}',end=' ' if ("
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "765c0a59-d803-425b-8de5-800f07c52d14",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  16.077  99.598  25.983   4.704   1.033  81.667   2.547  95.751  47.192  60.623\n",
      "  18.601   3.748   3.300  38.438  73.706  54.239  84.288  95.805  12.129  26.392\n",
      "  36.123  18.318  99.530  67.563  23.833  98.766  98.394  40.953  28.483  70.253\n",
      "  82.636  74.792   0.123  87.939  93.547   2.080   9.027  52.074  34.099  25.948\n",
      "  58.727  18.654  84.056  62.729  10.633  76.259  20.811  58.713  55.751  14.830\n",
      "  23.496  66.793  18.886  54.424   0.081  81.123  18.655  29.516  44.990  31.016\n",
      "  62.671  29.842  72.128  96.979  87.554  46.856  91.486  33.781   6.842  36.456\n",
      "  14.070  62.079   5.674  61.536  19.655   0.024  62.851  75.856   4.984  40.118\n",
      "  20.680  63.351  46.459   7.065  41.934  10.597  38.525  11.907  43.128  35.827\n",
      "  99.671  29.417  41.710  41.732  44.802   8.238  94.268  12.730  48.154  53.083\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "for i in range(100):\n",
    "    print('%8.3f' % (random.random()*100), end='')\n",
    "    if ( (i+1) %10 ==0):\n",
    "        print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d4e6bc05-4165-4bc2-a043-bf68806f3605",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
